/** @odoo-module */

import { Todo } from "../todo/todo"
import { useAutofocus } from "../utils" //引用utils中的函数useAutofocus

import { Component, useState } from "@odoo/owl"

export class TodoList extends Component {
  setup() {
    // 初始化
    this.nextId = 0
    this.todoList = useState([])
    useAutofocus("todoListInput")
  }
  // 函数，添加待办事项
  addTodo(ev) {
    // keyCode键码 13代表回车键
    // ev.target代表当前厨房事件的DOM元素。因此ev.target.value表示输入框输入的值
    if(ev.keyCode === 13 && ev.target.value != "") {
      this.todoList.push({id: this.nextId++, description: ev.target.value, done: false})
      ev.target.value = "" //清空输入框的内容
    }
  }

  toggleTodo(todoId) {
    console.log(todoId)
    const todo = this.todoList.find((todo) => todo.id === todoId)
    if (todo) {
      todo.done = !todo.done
    }
  }
  removeTodo(todoId) {
    const todoIndex = this.todoList.findIndex((todo) => todo.id === todoId)
    if(todoIndex >= 0) {
      this.todoList.splice(todoIndex, 1)
    }
  }
}

TodoList.components = { Todo };
TodoList.template = "todoList.TodoList"